<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
function pr($data=array(), $end='', $stop=false)
{
    print_r($data);
    echo $end;
    if($stop) die;
}
function pd($data=array(), $end='', $stop=true)
{
    print_r($data);
    echo $end;
    if($stop) die;
}
function get_username($id){
    $u=User::model()->findByPk($id);
    if(isset($u->username)){
        return $u->username;
    }
}

function getGroupUser($yii){
    if(empty($yii->user->id)){
        return array();
    }
    $user_r = array();

    $tk = Authassignment::model()->findByAttributes(array('itemname'=>'Admin', 'userid'=>$yii->user->id));
    if(!empty($tk)){
        $users = User::model()->with(array('profile'))->findAll();
        foreach($users as $user){
            $user_r[$user->id]=$user->profile->first_name.$user->profile->last_name;
        }
        asort($user_r);
        return $user_r;
    }

    $user_list = $yii->db->createCommand('SELECT distinct(`g`.`uid`) FROM `blog_user_group` as `g` RIGHT JOIN (SELECT `gid` FROM `blog_user_group` WHERE `uid` = '.
    $yii->user->id.') AS `gg` ON `g`.`gid`=`gg`.`gid`')->queryAll();

    if(empty($user_list)){
        $u=User::model()->with(array('profile'))->findByPk($yii->user->id);
        $user_r[$u->id]=$u->profile->first_name.$u->profile->last_name;
    }else{
        foreach($user_list as $user){
            $u=User::model()->with(array('profile'))->findByPk($user['uid']);
            $user_r[$u->id]=$u->profile->first_name.$u->profile->last_name;
        }
    }

    asort($user_r);
    return $user_r;
}

function get_menu($id=0){
    if($id>0){
        $m=Menu::model()->findByPk($id);
        if(isset($m->menu_name)){
            return $m->menu_name;
        }
    }elseif($id==0){
        return '默认分类';
    }else{
        $m = Menu::model()->findAll();
        $m_r = array();
        foreach($m as $m_one){
            $m_r[$m_one->menu_id] = $m_one->menu_name;
        }
        return $m_r;
    }
}

class Controller extends RController
{
	/**
	 * @var string the default layout for the controller view. Defaults to 'column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
}